# //div[@class="listview full"]//tbody/tr/td[1]/text()  序号
# //div[@class="listview full"]//tbody/tr/td[2]//text()  代码
# //div[@class="listview full"]//tbody/tr/td[3]//text()  名称
# //table/tbody/tr/td[4]/a[1]/text()  相关链接（股吧）
# //table/tbody/tr/td[4]/a[1]/@href
# //table/tbody/tr/td[4]/a[2]/text()  相关链接（资金流）
# //table/tbody/tr/td[4]/a[2]/@href
# //table/tbody/tr/td[4]/a[3]/text()  相关链接（数据）
# //table/tbody/tr/td[4]/a[3]/@href
# //table/tbody/tr/td[5]/span/text()  最新价
# //table/tbody/tr/td[6]/span/text()  涨跌幅
# //table/tbody/tr/td[7]/span/text()  涨跌额
# //table/tbody/tr/td[8]/text()       成交量
# //table/tbody/tr/td[9]/text()       成交额
# //table/tbody/tr/td[10]/text()      振幅
# //table/tbody/tr/td[11]//text()     最高
# //table/tbody/tr/td[12]//text()     最低
# //table/tbody/tr/td[13]//text()     今开
# //table/tbody/tr/td[14]//text()     昨收
# //table/tbody/tr/td[15]//text()     换手率
# //table/tbody/tr/td[16]//text()     市盈率
# //table/tbody/tr/td[17]//text()     市净率

# import urllib.request
# import jsonpath
# import json
#
# url = 'http://28.push2.eastmoney.com/api/qt/clist/get?cb=jQuery11240789491577816885_1600509526616&pn=1&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1600509526626'
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
# }

# request = urllib.request.Request(url=url, headers=headers)
# response = urllib.request.urlopen(request)
# content = response.read().decode('utf-8')
# content = content.split("(")[1]
# content = content.split(")")[0]
# # print(type(content))
# # print(content)
# with open('股票.json', 'w', encoding='utf-8') as fp:
#     fp.write(content)
#
# obj = json.load(open('股票.json', 'r', encoding='utf-8'))
# stock_code_list = jsonpath.jsonpath(obj, '$.data.diff[*].f12')
# name_list = jsonpath.jsonpath(obj, '$.data.diff[*].f14')
# the_latest_price_list = jsonpath.jsonpath(obj, '$.data.diff[*].f2')
# increase_and_decrease_list = jsonpath.jsonpath(obj, '$.data.diff[*].f3')
# for i in range(len(stock_code_list)):
#     stock_code = stock_code_list[i]
#     name = name_list[i]
#     the_latest_price = the_latest_price_list[i]
#     increase_and_decrease = increase_and_decrease_list[i]
#     print(i+1, stock_code, name, the_latest_price)

from selenium import webdriver
from lxml import etree

path = 'phantomjs.exe'

browser = webdriver.PhantomJS(path)

url = 'http://quote.eastmoney.com/center/gridlist.html?st=ChangePercent&sortType=C&sortRule=-1'

browser.get(url)

content = browser.page_source
# print(content)

tree = etree.HTML(content)

index_list = tree.xpath('//div[@class="listview full"]//tbody/tr/td[1]/text()')
stock_code_list = tree.xpath('//div[@class="listview full"]//tbody/tr/td[2]//text()')
name_list = tree.xpath('//div[@class="listview full"]//tbody/tr/td[3]//text()')

for i in range(len(index_list)):
    index = index_list[i]
    stock_code = stock_code_list[i]
    name = name_list[i]
    print(index, stock_code, name)
browser.quit()